
package com.bi.design.mqtt.borker.auth;

import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.digest.DigestUtil;
import org.springframework.stereotype.Component;

import java.security.interfaces.RSAPrivateKey;

/**
 * 用户名和密码认证服务
 */
@Component
public class AuthService implements IAuthService {

    private RSAPrivateKey privateKey;

    public static final String PWD = "a66abb5684c45962d887564f08346e8d";

    @Override
    public boolean checkValid(String username, String password) {
//        if (StrUtil.isBlank(username)) return false;
//        if (StrUtil.isBlank(password)) return false;
//        RSA rsa = new RSA(privateKey, null);
//        String value = rsa.encryptBcd(username, KeyType.PrivateKey);
//        return value.equals(password) ? true : false;
        return checkAuthentication(username, password);
        //        return value.equals(password) ? true : false;
    }


    private boolean checkAuthentication(String username, String password) {
        String md5Hex = DigestUtil.md5Hex(username + password);
        return md5Hex.equals(PWD);
    }

    //    @PostConstruct
    public void init() {
        privateKey = IoUtil.readObj(AuthService.class.getClassLoader().getResourceAsStream("keystore/auth-private.key"));
    }

    public static void main(String[] args) {
        System.out.println(DigestUtil.md5Hex("admin123456"));
    }
}
